/*
 *  Copyright 2013 National Institute of Advanced Industrial Science and Technology
 *  
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *  
 *      http://www.apache.org/licenses/LICENSE-2.0
 *  
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package org.sss.common.io;

import static org.sss.common.Utils.wrap;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sss.common.DataReceiver;
import org.sss.common.KeyValues;
import org.sss.mapreduce.Configuration;
import org.sss.mapreduce.DB;
import org.sss.mapreduce.GroupID;
import org.sss.mapreduce.SssException;
import org.sss.mapreduce.StorageNode;
import org.sss.util.BytesComparator;

public class RestrictedWholeDataKeyValueReader extends AbstractKeyValueReader {
  private static Logger logger = LoggerFactory.getLogger(RestrictedWholeDataKeyValueReader.class);
  private static BytesComparator CMP = new BytesComparator();

  public RestrictedWholeDataKeyValueReader(Configuration conf,
                                           StorageNode node,
                                           GroupID source,
                                           int subhash,
                                           DataReceiver<List<KeyValues>> next) throws SssException {
    super(conf, node, source, subhash, next);
  }

  @Override
  protected void doRead(IOThread iot)
    throws SssException, IOException, InterruptedException {
    assert getSource() != null: "source should not be null";
    DB db = iot.getDB(node);
    int count_max = iot.getIOGetBatchCount();
    int limitNBytes = iot.getIOGetLimitNBytes();

    final KeyFormatter keyFormatter = KeyFormatter.get(getSource().getKeyFormat());
    assert keyFormatter == KeyFormatter.processingOrderFormatter: "KeyFormatter is not processingOrderFormatter.";
    final byte[] prefix = KeyFormatter.getPrefix(getSource(), subhash);
    final List<byte[]> allkeys = db.fwmkeys(prefix, -1);
    final int count_target = allkeys.size();
    boolean done = count_target == 0;
    int count_sofar = 0;
    final Map<byte[], List<byte[]>> multimap = new TreeMap<byte[], List<byte[]>>(CMP);
    while (!done) {
      checkCanceled();
      final int count_tmp = Math.min(count_target - count_sofar, count_max);
      byte[][] ks = new byte[count_tmp][];
      for (int i = 0; i < count_tmp; ++i) {
        ks[i] = allkeys.get(count_sofar + i);
        allkeys.set(count_sofar + i, null); // for gc
      }
      List<byte[]> kvs = db.getlist(ks);
      countup(kvs);
      final int count_read = kvs.size();
      logger.debug("count_read = {}", count_read);
      count_sofar += count_read / 2;
      if (isDestructiveRead() && count_read > 0) {
        db.outlist(ks);
      }
      done = (count_target == count_sofar);
      for (int i = 0; i < count_read; i += 2) {
        byte[] userkey = keyFormatter.getUserKey(kvs.get(i));
        List<byte[]> vs = multimap.get(userkey);
        if (vs == null) {
          vs = new ArrayList<byte[]>();
          multimap.put(userkey, vs);
        }
        vs.add(kvs.get(i + 1));
      }
    }

    List<KeyValues> kvss = new ArrayList<KeyValues>();
    int nbytes = 0;
    for (Map.Entry<byte[], List<byte[]>> e: multimap.entrySet()) {
      kvss.add(new KeyValues(e.getKey(), wrap(e.getValue().iterator())));
      for (byte[] v: e.getValue()) {
        nbytes += e.getKey().length + v.length;
      }
      if (nbytes >= limitNBytes) {
        next.put(kvss);
        kvss = new ArrayList<KeyValues>();
      }
    }
    if (!kvss.isEmpty()) {
      next.put(kvss);
    }
  }
}

